﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Xml.Linq;
using System;
using System.IO;
using System.Windows.Input;

namespace MidwayBattle
{
    /// <summary>
    /// Logique d'interaction pour WindowRemovePlayer.xaml
    /// </summary>
    public partial class WindowRemovePlayer : Window
    {
        private string Pass;
        private MainWindow Mw;

        public WindowRemovePlayer(MainWindow Mw)
        {
            InitializeComponent();
            this.MinHeight = 300;
            this.MinWidth = 300;
            this.Mw = Mw;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MessageBox.Show("A file with your password has been generated on your desktop", "Removal player", MessageBoxButton.OK, MessageBoxImage.Warning);
                string text = "Your password : " + Pass;
                string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                File.WriteAllText(mydocpath + @"\YourPassword.txt", text);
            }
            catch (Exception Iox)
            {
                MessageBox.Show(Iox.Message, "Removal player", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                XDocument doc = XDocument.Load("Users.xml");
                string Lg = this.textBox1.Text.ToUpper();
                string Pwd = this.passwordBox1.Password;
                IEnumerable<XElement> rech =
                         from el in doc.Root.Elements("Users")
                         where (string)el.Element("UserLog") == Lg
                         select el;

                if (rech.Count() != 0)
                {
                    foreach (XElement el in rech)
                    {
                        if ((string)el.Element("UserPassword") == Pwd)
                        {

                            el.Remove();
                            doc.Save("Users.xml");
                            MessageBox.Show("The player was removed successfully", "Removal player", MessageBoxButton.OK, MessageBoxImage.Information);
                            this.Mw.DisableMenuItems();
                            this.Close();
                        }
                        else
                        {
                            Pass = (string)el.Element("UserPassword");
                            this.button3.Visibility = Visibility;
                            MessageBox.Show("Incorrect password", "Removal player", MessageBoxButton.OK, MessageBoxImage.Warning);
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Incorrect login", "Removal player", MessageBoxButton.OK, MessageBoxImage.Warning);
                    this.textBox1.Text = "Pseudo";
                }
            }
            catch (FileNotFoundException Fnf)
            {
                MessageBox.Show(Fnf.Message + " A new file will be created but you will have to create a new profile.", "Removal player", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Clear_GotMouseCapture(object sender, MouseEventArgs e)
        {
            if (sender.Equals(textBox1))
            {
                this.textBox1.Clear();
            }
            else
            {
                this.passwordBox1.Clear();
            }
        }
    }
}
